"""
Case Type   : Operator
Case Name   : 创建操作符时merges存在，支持融合连接
Create At   : 2024.10.8
Owner       : wang-tianjing1
Description :
    1、创建实现操作符的函数
    2、创建操作符
    3、清理环境
Expect      :
    1、成功
    2、成功
    3、成功
History     :
"""

import os
import unittest

from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import Node


class OpreatorTest(unittest.TestCase):

    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.common = Common()
        self.sh_primary = CommonSH('dbuser')
        self.user_node = Node('dbuser')
        self.constant = Constant()

    def test_dolphin(self):
        text = '----step1:创建实现操作符的函数；expect：创建成功----'
        self.log.info(text)
        sql_cmd = f'drop function if exists test_procedure() cascade;'\
                  f'create function test_procedure(a int, b int) return bool as\n'\
                  f'begin\n'\
                  f'if a > b then return t;\n'\
                  f'else return f;\n'\
                  f'end if;\n'\
                  f'end;\n'
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.assertIn('DROP FUNCTION', sql_res, '执行失败' + text)
        self.assertIn('CREATE FUNCTION', sql_res, '执行失败' + text)

        sql_cmd2 = f'drop function if exists test_procedure1() cascade;' \
                  f'create function test_procedure1(a int, b int) return bool as\n' \
                  f'begin\n' \
                  f'if a < b then return t;\n' \
                  f'else return f;\n' \
                  f'end if;\n' \
                  f'end;\n'
        self.log.info(sql_cmd2)
        sql_res2 = self.sh_primary.execut_db_sql(sql_cmd2)
        self.log.info(sql_res2)
        self.assertIn('DROP FUNCTION', sql_res2, '执行失败' + text)
        self.assertIn('CREATE FUNCTION', sql_res2, '执行失败' + text)

        text = '----step2:创建操作符;expect:成功----'
        self.log.info(text)
        sql_cmd3 = f'create operator @!@('\
                   f'leftarg = int,'\
                   f'rightarg = int,'\
                   f'procedure = test_procedure,'\
                   f'commutator = @!@,'\
                   f'negator = !@!@,'\
                   f'hashes, merges);'
        self.log.info(sql_cmd3)
        sql_res3 = self.sh_primary.execut_db_sql(sql_cmd3)
        self.log.info(sql_res3)
        self.assertIn("CREATE OPERATOR", sql_res3,'执行失败' + text)

        sql_cmd4 = f'create operator &&&&(' \
                   f'leftarg = int,' \
                   f'rightarg = int,' \
                   f'procedure = test_procedure1,' \
                   f'commutator = operator(@!@),' \
                   f'hashes, merges);'
        self.log.info(sql_cmd4)
        sql_res4 = self.sh_primary.execut_db_sql(sql_cmd4)
        self.log.info(sql_res4)
        self.assertIn("CREATE OPERATOR", sql_res4, '执行失败' + text)

    def tearDown(self):
        text = '--step3:清理环境;expect:成功--'
        self.log.info(text)
        sql_cmd = f"drop function test_procedure() cascade;"\
                  f'drop function test_procedure1() cascade;'\
                  f'''delete from pg_operator where oprname = '!@!@';'''
        sql_res = self.sh_primary.execut_db_sql(sql_cmd)
        self.assertIn("DROP FUNCTION", sql_res, '执行失败' + text)
        self.assertIn("DELETE 1", sql_res, '执行失败' + text)
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
